
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>集合 · Dart编程指南</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="arcticfox">
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-anchor-navigation-ex/style/plugin.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-pageview-count/plugin.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-theme-comscore/test.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="URI处理.html" />
    
    
    <link rel="prev" href="字符串.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    
    
        
        <li>
            <a href="http://www.bczl.xyz/" target="_blank" class="custom-link">主页</a>
        </li>
    
    

    
    <li class="divider"></li>
    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    前言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="基础篇.html">
            
                <a href="基础篇.html">
            
                    
                    基础篇
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="环境搭建.html">
            
                <a href="环境搭建.html">
            
                    
                    环境搭建
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="简单语法.html">
            
                <a href="简单语法.html">
            
                    
                    简单语法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="函数.html">
            
                <a href="函数.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="进阶篇.html">
            
                <a href="进阶篇.html">
            
                    
                    进阶篇
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="面向对象.html">
            
                <a href="面向对象.html">
            
                    
                    面向对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="泛型.html">
            
                <a href="泛型.html">
            
                    
                    泛型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="枚举.html">
            
                <a href="枚举.html">
            
                    
                    枚举
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="库与导入.html">
            
                <a href="库与导入.html">
            
                    
                    库与导入
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="异步编程.html">
            
                <a href="异步编程.html">
            
                    
                    异步编程
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.5.1" data-path="异步编程之Future.html">
            
                <a href="异步编程之Future.html">
            
                    
                    Future
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5.2" data-path="异步编程之Stream.html">
            
                <a href="异步编程之Stream.html">
            
                    
                    Stream
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5.3" data-path="异步编程之Isolate.html">
            
                <a href="异步编程之Isolate.html">
            
                    
                    Isolate
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="异常处理.html">
            
                <a href="异常处理.html">
            
                    
                    异常处理与 Zone
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="生成器.html">
            
                <a href="生成器.html">
            
                    
                    生成器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.8" data-path="标准流与文件.html">
            
                <a href="标准流与文件.html">
            
                    
                    标准流与文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.9" data-path="运算符.html">
            
                <a href="运算符.html">
            
                    
                    附：运算符
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="常用标准库.html">
            
                <a href="常用标准库.html">
            
                    
                    常用标准库
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="字符串.html">
            
                <a href="字符串.html">
            
                    
                    字符串
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.4.2" data-path="集合.html">
            
                <a href="集合.html">
            
                    
                    集合
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="URI处理.html">
            
                <a href="URI处理.html">
            
                    
                    URI处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="日期和时间.html">
            
                <a href="日期和时间.html">
            
                    
                    日期和时间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="环境及外部调用.html">
            
                <a href="环境及外部调用.html">
            
                    
                    环境及外部调用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="其他.html">
            
                <a href="其他.html">
            
                    
                    其他
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="实战篇.html">
            
                <a href="实战篇.html">
            
                    
                    实战篇
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="网络编程.html">
            
                <a href="网络编程.html">
            
                    
                    网络编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="爬虫实战.html">
            
                <a href="爬虫实战.html">
            
                    
                    爬虫实战
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="HTTP服务器.html">
            
                <a href="HTTP服务器.html">
            
                    
                    HTTP服务器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="WebSocket服务器.html">
            
                <a href="WebSocket服务器.html">
            
                    
                    WebSocket服务器
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" >
            
                <span>
            
                    
                    附录
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="dart2.14.html">
            
                <a href="dart2.14.html">
            
                    
                    升级到Dart 2.14
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="http://www.bczl.xyz/" target="blank" class="gitbook-link">
            编程之路
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >集合</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <div id="anchor-navigation-ex-navbar"><i class="fa fa-navicon"></i><ul><ul><li><span class="title-icon "></span><a href="#&#x96C6;&#x5408;collections"><b></b>&#x96C6;&#x5408;(Collections)</a></li><ul><li><span class="title-icon "></span><a href="#list&#xFF08;&#x5217;&#x8868;&#xFF09;"><b></b>List&#xFF08;&#x5217;&#x8868;&#xFF09;</a></li><li><span class="title-icon "></span><a href="#&#x6620;&#x5C04;&#xFF08;map&#xFF09;"><b></b>&#x6620;&#x5C04;&#xFF08;Map&#xFF09;</a></li><li><span class="title-icon "></span><a href="#&#x96C6;&#x5408;&#xFF08;set&#xFF09;"><b></b>&#x96C6;&#x5408;&#xFF08;Set&#xFF09;</a></li><li><span class="title-icon "></span><a href="#&#x96C6;&#x5408;&#x7C7B;&#x578B;&#x7684;&#x901A;&#x7528;&#x65B9;&#x6CD5;"><b></b>&#x96C6;&#x5408;&#x7C7B;&#x578B;&#x7684;&#x901A;&#x7528;&#x65B9;&#x6CD5;</a></li></ul></ul></ul></div><a href="#" id="anchorNavigationExGoTop"><i class="fa fa-arrow-up"></i></a><!-- ex_nolevel -->
<h2 id="&#x96C6;&#x5408;collections"><a name="&#x96C6;&#x5408;collections" class="anchor-navigation-ex-anchor" href="#&#x96C6;&#x5408;collections"><i class="fa fa-link" aria-hidden="true"></i></a>&#x96C6;&#x5408;(Collections)</h2>
<p>Dart &#x5E26;&#x6709;&#x4E00;&#x4E2A;&#x6838;&#x5FC3;&#x96C6;&#x5408;API&#xFF0C;&#x5176;&#x4E2D;&#x5305;&#x62EC;&#x5217;&#x8868;&#xFF08;list&#xFF09;&#x3001;&#x96C6;&#x5408;&#xFF08;set&#xFF09;&#x548C;&#x6620;&#x5C04;&#xFF08;map&#xFF09;&#x7684;&#x7C7B;</p>
<h3 id="list&#xFF08;&#x5217;&#x8868;&#xFF09;"><a name="list&#xFF08;&#x5217;&#x8868;&#xFF09;" class="anchor-navigation-ex-anchor" href="#list&#xFF08;&#x5217;&#x8868;&#xFF09;"><i class="fa fa-link" aria-hidden="true"></i></a>List&#xFF08;&#x5217;&#x8868;&#xFF09;</h3>
<pre><code class="lang-dart">    <span class="hljs-comment">// 1.&#x521B;&#x5EFA;&#x5217;&#x8868;</span>
    <span class="hljs-comment">// 1) &#x5B57;&#x9762;&#x91CF;&#x521B;&#x5EFA;</span>
    <span class="hljs-keyword">var</span> list1 = [];
    <span class="hljs-built_in">List</span> list2 = [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-string">&quot;a&quot;</span>,<span class="hljs-string">&quot;dart&quot;</span>];

    <span class="hljs-comment">// 2) &#x901A;&#x8FC7;&#x7C7B;&#x6784;&#x9020;&#x65B9;&#x6CD5;&#x521B;&#x5EFA;</span>
    <span class="hljs-keyword">var</span> list3 = <span class="hljs-built_in">List</span>();

    <span class="hljs-comment">// 2. &#x57FA;&#x672C;&#x64CD;&#x4F5C;</span>
    <span class="hljs-comment">// 1) &#x589E;</span>
    list1.add(<span class="hljs-string">&quot;value&quot;</span>);
    list1.add(<span class="hljs-number">88</span>);

    list3.insert(<span class="hljs-number">0</span>, <span class="hljs-number">1</span>);
    list3.insert(<span class="hljs-number">0</span>, <span class="hljs-number">2</span>);
    list3.insert(<span class="hljs-number">1</span>, <span class="hljs-number">3</span>);

    <span class="hljs-comment">// 2) &#x5220;&#x9664;</span>
    list2.remove(<span class="hljs-string">&quot;dart&quot;</span>);
    list2.removeAt(<span class="hljs-number">0</span>);
    list2.removeLast();

    <span class="hljs-comment">// 3) &#x4FEE;&#x6539;</span>
    list2.replaceRange(<span class="hljs-number">0</span>, <span class="hljs-number">2</span>, [<span class="hljs-string">&quot;Java&quot;</span>,<span class="hljs-string">&quot;C&quot;</span>]);

    <span class="hljs-comment">// 4) &#x67E5;&#x770B;</span>
    <span class="hljs-built_in">print</span>(list2[<span class="hljs-number">0</span>]);
    <span class="hljs-built_in">print</span>(list2[<span class="hljs-number">1</span>]);
    <span class="hljs-built_in">print</span>(list2.first);
    <span class="hljs-built_in">print</span>(list2.last);
    <span class="hljs-built_in">print</span>(list2.length);
    <span class="hljs-built_in">print</span>(list2.indexOf(<span class="hljs-number">2</span>) == <span class="hljs-number">0</span>);

    <span class="hljs-comment">// 3. &#x5176;&#x4ED6;&#x64CD;&#x4F5C;</span>
    <span class="hljs-comment">// &#x62FC;&#x63A5;&#x6570;&#x7EC4;&#x5143;&#x7D20;</span>
    <span class="hljs-built_in">print</span>(list2.join(<span class="hljs-string">&quot;&#xFF0C;&quot;</span>));

    <span class="hljs-comment">// &#x5217;&#x8868;&#x5408;&#x5E76;</span>
    <span class="hljs-keyword">var</span> tmpList = [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>];
    <span class="hljs-comment">// print(list2 + tmpList);  &#x4F7F;&#x7528;&quot;+&quot;&#x64CD;&#x4F5C;&#x7B26;</span>
    list2.addAll(tmpList);
    <span class="hljs-built_in">print</span>(list2);
</code></pre>
<p>&#x5217;&#x8868;&#x7684;6&#x79CD;&#x6784;&#x9020;&#x65B9;&#x6CD5;</p>
<ul>
<li><p><del><code>List([int length])</code></del>(&#x8FC7;&#x65F6;&#xFF0C;&#x53C2;&#x8003; <a href="dart2.14.html">&#x5347;&#x7EA7;&#x5230;Dart 2.14</a>)
&#x521B;&#x5EFA;&#x7ED9;&#x5B9A;&#x957F;&#x5EA6;&#x7684;&#x5217;&#x8868;</p>
</li>
<li><p><code>List.filled(int length, E fill, {bool growable: false})</code>
&#x521B;&#x5EFA;&#x7ED9;&#x5B9A;&#x957F;&#x5EA6;&#x5E76;&#x6307;&#x5B9A;&#x586B;&#x5145;&#x5143;&#x7D20;&#x7684;&#x5217;&#x8868;</p>
</li>
<li><p><code>List.from(Iterable elements, {bool growable: true})</code>
&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x5305;&#x542B;&#x6240;&#x6709;&#x53EF;&#x8FED;&#x4EE3;&#x5143;&#x7D20;&#x7684;&#x5217;&#x8868;</p>
</li>
<li><p><code>List.generate(int length, E generator(int index), {bool growable: true})</code>
&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x5305;&#x542B;&#x6240;&#x6709;&#x751F;&#x6210;&#x5668;&#x8FD4;&#x56DE;&#x503C;&#x7684;&#x5217;&#x8868;</p>
</li>
<li><p><code>List.of(Iterable&lt;E&gt; elements, {bool growable: true})</code>
&#x4ECE;&#x7ED9;&#x5B9A;&#x7684;&#x53EF;&#x8FED;&#x4EE3;&#x5143;&#x7D20;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x5217;&#x8868;</p>
</li>
<li><p><code>List.unmodifiable(Iterable elements)</code>
&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x5305;&#x542B;&#x6240;&#x6709;&#x53EF;&#x8FED;&#x4EE3;&#x5143;&#x7D20;&#x7684;&#x4E0D;&#x53EF;&#x4FEE;&#x6539;&#x7684;&#x5217;&#x8868;</p>
</li>
</ul>
<p>&#x793A;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-dart">  <span class="hljs-comment">// &#x4ECE;&#x5DF2;&#x6709;&#x7684;&#x5217;&#x8868;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x5217;&#x8868; of&#x3001;from</span>
  <span class="hljs-built_in">List</span>&lt;<span class="hljs-built_in">int</span>&gt; list1 = &lt;<span class="hljs-built_in">int</span>&gt;[<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>];
  <span class="hljs-keyword">var</span> list2 = <span class="hljs-built_in">List</span>.from(list1);
  <span class="hljs-keyword">var</span> list3 = <span class="hljs-built_in">List</span>.of(list1);
  <span class="hljs-comment">// &#x67E5;&#x770B;of&#x548C;from&#x7684;&#x533A;&#x522B;</span>
  <span class="hljs-built_in">print</span>(list2.runtimeType);
  <span class="hljs-built_in">print</span>(list3.runtimeType);

  <span class="hljs-comment">// &#x6307;&#x5B9A;&#x586B;&#x5145;&#x5143;&#x7D20;</span>
  <span class="hljs-keyword">var</span> list4 = <span class="hljs-built_in">List</span>.filled(<span class="hljs-number">5</span>, <span class="hljs-number">1</span>);
  <span class="hljs-built_in">print</span>(list4);

  <span class="hljs-comment">// &#x6839;&#x636E;&#x6761;&#x4EF6;&#x751F;&#x6210;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x5217;&#x8868;&#xFF1A; [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]</span>
  <span class="hljs-keyword">var</span> list5 = <span class="hljs-built_in">List</span>.generate(<span class="hljs-number">10</span>, (i){
    <span class="hljs-keyword">return</span> (i*<span class="hljs-number">2</span>).toString();
  });

  <span class="hljs-keyword">var</span> list6 = <span class="hljs-built_in">List</span>.unmodifiable([<span class="hljs-number">0</span>,<span class="hljs-number">2</span>,<span class="hljs-number">4</span>,<span class="hljs-number">6</span>,<span class="hljs-number">8</span>]);
  <span class="hljs-comment">// &#x4FEE;&#x6539;&#x5217;&#x8868;&#x4F1A;&#x62A5;&#x9519;&#xFF1A;Unsupported operation: Cannot add to an unmodifiable list</span>
  <span class="hljs-comment">// list6[0] = 99; </span>
  <span class="hljs-comment">// list6.add(100);</span>
</code></pre>
<p>&#x6CE8;&#x610F;&#xFF1A;</p>
<ol>
<li><code>of</code>&#x548C;<code>from</code>&#x4E24;&#x4E2A;&#x6784;&#x9020;&#x65B9;&#x6CD5;&#x7684;&#x533A;&#x522B;</li>
<li>&#x6784;&#x9020;&#x65B9;&#x6CD5;&#x4E2D;<code>growable</code>&#x53C2;&#x6570;&#x7684;&#x4F5C;&#x7528;</li>
<li>&#x4F7F;&#x7528;<code>filled</code>&#x6784;&#x9020;&#x65B9;&#x6CD5;&#x5C06;&#x5BF9;&#x8C61;&#x7C7B;&#x578B;&#x4F5C;&#x4E3A;&#x586B;&#x5145;&#x5143;&#x7D20;&#x7684;&#x5751;</li>
</ol>
<h4 id="&#x5217;&#x8868;&#x7684;&#x5176;&#x4ED6;&#x64CD;&#x4F5C;"><a name="&#x5217;&#x8868;&#x7684;&#x5176;&#x4ED6;&#x64CD;&#x4F5C;" class="anchor-navigation-ex-anchor" href="#&#x5217;&#x8868;&#x7684;&#x5176;&#x4ED6;&#x64CD;&#x4F5C;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x5217;&#x8868;&#x7684;&#x5176;&#x4ED6;&#x64CD;&#x4F5C;</h4>
<pre><code class="lang-dart">  <span class="hljs-comment">// &#x6839;&#x636E;&#x6761;&#x4EF6;&#x4ECE;&#x5217;&#x8868;&#x4E2D;&#x7B5B;&#x9009;&#x5143;&#x7D20;</span>
  <span class="hljs-keyword">var</span> list7 = [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-string">&quot;4&quot;</span>,<span class="hljs-string">&quot;5&quot;</span>,<span class="hljs-string">&quot;6&quot;</span>].where((el){
    <span class="hljs-keyword">if</span>(el.runtimeType == <span class="hljs-built_in">String</span>) <span class="hljs-keyword">return</span> <span class="hljs-keyword">true</span>;
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">false</span>;
  });
  <span class="hljs-built_in">print</span>(list7.toList());

  <span class="hljs-comment">// &#x5217;&#x8868;&#x5143;&#x7D20;&#x7684;&#x7C7B;&#x578B;&#x8F6C;&#x6362;</span>
  <span class="hljs-keyword">var</span> list8 = [<span class="hljs-number">2</span>,<span class="hljs-number">4</span>,<span class="hljs-number">6</span>,<span class="hljs-number">8</span>];
  <span class="hljs-keyword">var</span> list9 = list8.map((el)=&gt; el.toDouble());
  <span class="hljs-built_in">print</span>(list9.toList());

  <span class="hljs-comment">// &#x5217;&#x8868;&#x904D;&#x5386;</span>
  list9.forEach(<span class="hljs-built_in">print</span>);
</code></pre>
<p>&#x66F4;&#x591A;API &#x53C2;&#x89C1; Dart&#x8BED;&#x8A00;&#x7684; <a href="https://api.dart.dev/stable/2.7.1/dart-core/List-class.html" target="_blank">List&#x6587;&#x6863;</a></p>
<h3 id="&#x6620;&#x5C04;&#xFF08;map&#xFF09;"><a name="&#x6620;&#x5C04;&#xFF08;map&#xFF09;" class="anchor-navigation-ex-anchor" href="#&#x6620;&#x5C04;&#xFF08;map&#xFF09;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x6620;&#x5C04;&#xFF08;Map&#xFF09;</h3>
<p>&#x901A;&#x5E38;&#x88AB;&#x79F0;&#x4E3A;&#x5B57;&#x5178;&#x6216;&#x5173;&#x8054;&#x6570;&#x7EC4;&#xFF0C;&#x662F;<code>&#x952E;-&#x503C;&#x5BF9;</code>&#x7684;&#x65E0;&#x5E8F;&#x96C6;&#x5408;&#xFF0C;&#x76F8;&#x5F53;&#x4E8E;Java&#x4E2D;&#x7684;<code>HashMap</code>&#x3002;&#x4E0E;JavaScript&#x4E0D;&#x540C;&#xFF0C;Dart&#x7684;&#x5BF9;&#x8C61;&#x4E0D;&#x662F;&#x6620;&#x5C04;&#x3002;</p>
<ul>
<li><p><code>Map()</code>
&#x4F7F;&#x7528;&#x9ED8;&#x8BA4;&#x5B9E;&#x73B0; LinkedHashMap &#x521B;&#x5EFA;&#x4E00;&#x4E2A;Map&#x5B9E;&#x4F8B;</p>
</li>
<li><p><code>Map.from(Map other)</code>
&#x4ECE;&#x53E6;&#x4E00;&#x4E2A;Map&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x5305;&#x542B;&#x6240;&#x6709;&#x952E;&#x503C;&#x5BF9;&#x7684;&#x65B0;Map</p>
</li>
<li><p><code>Map.fromEntries(Iterable&lt;MapEntry&lt;K, V&gt;&gt; entries)</code>
&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x5305;&#x542B;&#x6240;&#x6709;entries&#x7684;&#x65B0;&#x7684;Map</p>
</li>
<li><p><code>Map.fromIterable(Iterable iterable, {K key(dynamic element), V value(dynamic element)})</code>
&#x521B;&#x5EFA;&#x4E00;&#x4E2A;Map&#x5B9E;&#x4F8B;&#xFF0C;&#x5176;&#x4E2D;&#x7684;&#x952E;&#x548C;&#x503C;&#x662F;&#x4ECE; iterable&#x4E2D;&#x8BA1;&#x7B97;&#x5F97;&#x5230;</p>
</li>
<li><p><code>Map.fromIterables(Iterable&lt;K&gt; keys, Iterable&lt;V&gt; values)</code>
&#x521B;&#x5EFA;&#x4E00;&#x4E2A;Map&#x5B9E;&#x4F8B;&#xFF0C;&#x5C06;&#x7ED9;&#x5B9A;&#x5173;&#x8054;keys&#x5230;values</p>
</li>
<li><p><code>Map.identity()</code>
&#x4F7F;&#x7528;&#x9ED8;&#x8BA4;&#x5B9E;&#x73B0;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x57FA;&#x4E8E;&#x8EAB;&#x4EFD;&#x7684;&#x6620;&#x5C04;&#x3002;</p>
</li>
<li><p><code>Map.of(Map&lt;K, V&gt; other)</code>
&#x4ECE;&#x53E6;&#x4E00;&#x4E2A;Map&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x65B0;&#x7684;Map&#x5BF9;&#x8C61;</p>
</li>
<li><p><code>Map.unmodifiable(Map other)</code>
&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x4E0D;&#x53EF;&#x4FEE;&#x6539;&#x7684;&#x6620;&#x5C04;</p>
</li>
</ul>
<p>Map&#x7684;&#x9ED8;&#x8BA4;&#x5B9E;&#x73B0;&#x7C7B;&#x662F;<code>LinkedHashMap</code>&#xFF0C;&#x9664;&#x6B64;&#x5916;&#xFF0C;&#x8FD8;&#x6709;<code>HashMap&#x3001; HttpSession&#x3001; LinkedHashMap&#x3001; MapMixin&#x3001; MapView</code>&#x7B49;&#x5B9E;&#x73B0;&#x7C7B;&#x3002;&#x5176;&#x4E2D;&#x6700;&#x5E38;&#x7528;&#x7684;&#x662F;<code>LinkedHashMap</code>&#x548C;<code>HashMap</code>&#xFF0C;&#x8FD9;&#x4E24;&#x8005;&#x7684;&#x4E3B;&#x8981;&#x533A;&#x522B;&#x5728;&#x4E8E;&#x4FDD;&#x5B58;&#x7684;&#x952E;&#x503C;&#x5BF9;&#x662F;&#x5426;&#x6709;&#x5E8F;&#xFF0C;&#x904D;&#x5386;&#x524D;&#x8005;&#x662F;&#x6709;&#x5E8F;&#x7684;&#xFF0C;&#x5982;&#x679C;&#x4F60;&#x4E0D;&#x5173;&#x5FC3;&#x952E;&#x503C;&#x5BF9;&#x7684;&#x63D2;&#x5165;&#x987A;&#x5E8F;&#xFF0C;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x4F7F;&#x7528;<code>HashMap</code>&#x3002;</p>
<pre><code class="lang-dart">  <span class="hljs-built_in">Map</span> m1 = {<span class="hljs-string">&quot;name&quot;</span>:<span class="hljs-string">&quot;Bruce&quot;</span>,
            <span class="hljs-string">&quot;number&quot;</span>:[<span class="hljs-number">110</span>,<span class="hljs-number">119</span>,<span class="hljs-number">120</span>],
            <span class="hljs-string">&quot;addr&quot;</span>:{<span class="hljs-string">&quot;d1&quot;</span>:<span class="hljs-string">&quot;xxx&quot;</span>}};  

  <span class="hljs-comment">// Map&#x7684;&#x904D;&#x5386;</span>
  m1.forEach((key,value){
     <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;$key = $value&quot;</span>);
  });

  <span class="hljs-comment">// &#x6839;&#x636E;&#x6761;&#x4EF6;&#x5220;&#x9664;&#x5143;&#x7D20;</span>
   m1.removeWhere((k,v){
     <span class="hljs-keyword">return</span> k == <span class="hljs-string">&quot;name&quot;</span>;
   });

   <span class="hljs-comment">// Map&#x5143;&#x7D20;&#x7684;&#x7C7B;&#x578B;&#x8F6C;&#x6362;</span>
  <span class="hljs-built_in">Map</span> m2 = &lt;<span class="hljs-built_in">String</span>,<span class="hljs-built_in">int</span>&gt;{<span class="hljs-string">&quot;a&quot;</span>:<span class="hljs-number">1</span>,<span class="hljs-string">&quot;b&quot;</span>:<span class="hljs-number">2</span>};
  <span class="hljs-built_in">print</span>(m2.runtimeType);

  <span class="hljs-keyword">var</span> m3 = m2.map((k,v){
    <span class="hljs-keyword">return</span> MapEntry&lt;<span class="hljs-built_in">String</span>,<span class="hljs-built_in">String</span>&gt;(k,v.toString());
  });
  <span class="hljs-built_in">print</span>(m3.runtimeType);
</code></pre>
<h3 id="&#x96C6;&#x5408;&#xFF08;set&#xFF09;"><a name="&#x96C6;&#x5408;&#xFF08;set&#xFF09;" class="anchor-navigation-ex-anchor" href="#&#x96C6;&#x5408;&#xFF08;set&#xFF09;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x96C6;&#x5408;&#xFF08;Set&#xFF09;</h3>
<p>Dart&#x4E2D;&#x7684;<code>Set</code>&#x4E0E;Java&#x7C7B;&#x4F3C;&#xFF0C;&#x662F;&#x4E00;&#x7EC4;&#x65E0;&#x5E8F;&#x7684;&#x96C6;&#x5408;&#xFF0C;&#x5176;&#x4E2D;&#x6BCF;&#x4E2A;&#x5143;&#x7D20;&#x53EA;&#x80FD;&#x51FA;&#x73B0;&#x4E00;&#x6B21;&#x3002;&#x56E0;&#x4E3A;&#x96C6;&#x5408;&#x662F;&#x65E0;&#x5E8F;&#x7684;&#xFF0C;&#x6240;&#x4EE5;&#x4E0D;&#x80FD;&#x901A;&#x8FC7;&#x7D22;&#x5F15;&#x83B7;&#x5F97;&#x96C6;&#x5408;&#x7684;&#x9879;&#x3002;</p>
<ul>
<li><p><code>Set()</code>
&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x7A7A;&#x7684;Set</p>
</li>
<li><p><code>Set.from(Iterable elements)</code>
&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x5305;&#x542B;&#x6240;&#x6709;Iterable&#x5143;&#x7D20;&#x7684;&#x96C6;&#x5408;</p>
</li>
<li><p><code>Set.identity()</code>
&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x7A7A;&#x7684;identity &#x96C6;&#x5408;</p>
</li>
<li><p><code>Set.of(Iterable&lt;E&gt; elements)</code>
&#x4ECE;&#x7ED9;&#x5B9A;&#x7684;Iterable&#x5143;&#x7D20;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x65B0;&#x96C6;&#x5408;</p>
</li>
</ul>
<pre><code class="lang-dart"><span class="hljs-keyword">var</span> mySets = <span class="hljs-built_in">Set</span>();
mySets.addAll([<span class="hljs-string">&apos;gold&apos;</span>, <span class="hljs-string">&apos;titanium&apos;</span>, <span class="hljs-string">&apos;xenon&apos;</span>]);
<span class="hljs-keyword">assert</span>(mySets.length == <span class="hljs-number">3</span>);

<span class="hljs-comment">// &#x6DFB;&#x52A0;&#x91CD;&#x590D;&#x5143;&#x7D20;&#x662F;&#x65E0;&#x6548;&#x7684;</span>
mySets.add(<span class="hljs-string">&apos;gold&apos;</span>);
<span class="hljs-keyword">assert</span>(mySets.length == <span class="hljs-number">3</span>);

<span class="hljs-comment">// &#x5220;&#x9664;&#x4E00;&#x4E2A;&#x5143;&#x7D20;</span>
mySets.remove(<span class="hljs-string">&apos;gold&apos;</span>);
<span class="hljs-keyword">assert</span>(mySets.length == <span class="hljs-number">2</span>);
</code></pre>
<p>&#x4F7F;&#x7528;<code>contains()</code>&#x548C;<code>containsAll()</code>&#x6765;&#x68C0;&#x67E5;&#x96C6;&#x5408;&#x4E2D;&#x662F;&#x5426;&#x5305;&#x542B;&#x4E00;&#x4E2A;&#x6216;&#x591A;&#x4E2A;&#x5143;&#x7D20;</p>
<pre><code class="lang-dart"><span class="hljs-keyword">var</span> mySets = <span class="hljs-built_in">Set</span>();
mySets.addAll([<span class="hljs-string">&apos;gold&apos;</span>, <span class="hljs-string">&apos;titanium&apos;</span>, <span class="hljs-string">&apos;xenon&apos;</span>]);

<span class="hljs-keyword">assert</span>(mySets.contains(<span class="hljs-string">&apos;titanium&apos;</span>));
<span class="hljs-keyword">assert</span>(mySets.containsAll([<span class="hljs-string">&apos;titanium&apos;</span>, <span class="hljs-string">&apos;xenon&apos;</span>]));
</code></pre>
<p>&#x4EA4;&#x96C6;&#x662F;&#x4E00;&#x4E2A;&#x96C6;&#x5408;&#xFF0C;&#x5176;&#x9879;&#x5728;&#x53E6;&#x5916;&#x4E24;&#x4E2A;&#x96C6;&#x5408;&#x4E2D;&#x3002;</p>
<pre><code class="lang-dart"><span class="hljs-keyword">var</span> mySets = <span class="hljs-built_in">Set</span>();
mySets.addAll([<span class="hljs-string">&apos;gold&apos;</span>, <span class="hljs-string">&apos;titanium&apos;</span>, <span class="hljs-string">&apos;xenon&apos;</span>]);

<span class="hljs-comment">// &#x521B;&#x5EFA;&#x4E24;&#x4E2A;&#x96C6;&#x5408;&#x7684;&#x4EA4;&#x96C6;</span>
<span class="hljs-keyword">var</span> nobleGases = <span class="hljs-built_in">Set</span>.from([<span class="hljs-string">&apos;xenon&apos;</span>, <span class="hljs-string">&apos;argon&apos;</span>]);
<span class="hljs-keyword">var</span> set2 = mySets.intersection(nobleGases);
<span class="hljs-keyword">assert</span>(set2.length == <span class="hljs-number">1</span>);
<span class="hljs-keyword">assert</span>(set2.contains(<span class="hljs-string">&apos;xenon&apos;</span>));
</code></pre>
<p>&#x5173;&#x4E8E;<code>Set</code>&#x7684;&#x66F4;&#x591A;&#x7528;&#x6CD5;&#xFF0C;&#x53C2;&#x89C1; <a href="https://api.dart.dev/stable/2.7.1/dart-core/Set-class.html" target="_blank">Set&#x6587;&#x6863;</a></p>
<h3 id="&#x96C6;&#x5408;&#x7C7B;&#x578B;&#x7684;&#x901A;&#x7528;&#x65B9;&#x6CD5;"><a name="&#x96C6;&#x5408;&#x7C7B;&#x578B;&#x7684;&#x901A;&#x7528;&#x65B9;&#x6CD5;" class="anchor-navigation-ex-anchor" href="#&#x96C6;&#x5408;&#x7C7B;&#x578B;&#x7684;&#x901A;&#x7528;&#x65B9;&#x6CD5;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x96C6;&#x5408;&#x7C7B;&#x578B;&#x7684;&#x901A;&#x7528;&#x65B9;&#x6CD5;</h3>
<pre><code class="lang-dart"><span class="hljs-comment">// &#x4F7F;&#x7528;isEmpty&#x6216;isNotEmpty&#x68C0;&#x67E5;&#x662F;&#x5426;&#x5305;&#x542B;&#x6709;&#x5143;&#x7D20;</span>
<span class="hljs-keyword">var</span> coffees = [];
<span class="hljs-keyword">var</span> teas = [<span class="hljs-string">&apos;green&apos;</span>, <span class="hljs-string">&apos;black&apos;</span>, <span class="hljs-string">&apos;chamomile&apos;</span>, <span class="hljs-string">&apos;earl grey&apos;</span>];
<span class="hljs-keyword">assert</span>(coffees.isEmpty);
<span class="hljs-keyword">assert</span>(teas.isNotEmpty);

<span class="hljs-comment">// &#x904D;&#x5386;.&#x6CE8;&#x610F;&#xFF0C;&#x5728;Map&#x4E0A;&#x8C03;&#x7528;&#x65F6;&#xFF0C;&#x51FD;&#x6570;&#x5FC5;&#x987B;&#x63A5;&#x53D7;&#x4E24;&#x4E2A;&#x53C2;&#x6570;(&#x952E;&#x548C;&#x503C;)</span>
teas.forEach((tea) =&gt; <span class="hljs-built_in">print</span>(<span class="hljs-string">&apos;I drink $tea&apos;</span>));

<span class="hljs-comment">// Iterable&#x63D0;&#x4F9B;map()&#x65B9;&#x6CD5;&#xFF0C;&#x53EF;&#x4EE5;&#x5728;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x4E2D;&#x63D0;&#x4F9B;&#x6240;&#x6709;&#x7ED3;&#x679C;</span>
<span class="hljs-keyword">var</span> loudTeas = teas.map((tea) =&gt; tea.toUpperCase());
loudTeas.forEach(<span class="hljs-built_in">print</span>);
</code></pre>
<blockquote>
<p>&#x6CE8;&#x610F;:map()&#x8FD4;&#x56DE;&#x7684;&#x5BF9;&#x8C61;&#x662F;&#x4E00;&#x4E2A;&#x53EF;&#x8FED;&#x4EE3;&#x7684;&#x3001;&#x5EF6;&#x8FDF;&#x6C42;&#x503C;&#x7684;&#x5BF9;&#x8C61;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x96C6;&#x5408;&#x5BF9;&#x8C61;&#x672C;&#x8EAB;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<code>map().toList()</code>&#x6216;<code>map().toSet()</code>&#x8FDB;&#x884C;&#x8F6C;&#x6362;</p>
</blockquote>
<pre><code class="lang-dart"><span class="hljs-keyword">var</span> loudTeas =
    teas.map((tea) =&gt; tea.toUpperCase()).toList();
</code></pre>
<hr>
<p><strong>&#x516C;&#x4F17;&#x53F7;&#x201C;&#x7F16;&#x7A0B;&#x4E4B;&#x8DEF;&#x4ECE;0&#x5230;1&#x201D;</strong></p>
<p><img src="https://gitee.com/arcticfox1919/ImageHosting/raw/master/img/20190301102949549.jpg" alt="20190301102949549"> </p>
<footer class="page-footer"><span class="copyright">Copyright &#xA9; Arcticfox 2020 all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x6587;&#x6863;&#x4FEE;&#x8BA2;&#x4E8E;&#xFF1A;
2021-10-18 20:50:20
</span></footer>
<script>console.log("plugin-popup....");document.onclick = function(e){ e.target.tagName === "IMG" && window.open(e.target.src,e.target.src)}</script><style>img{cursor:pointer}</style>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="字符串.html" class="navigation navigation-prev " aria-label="Previous page: 字符串">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="URI处理.html" class="navigation navigation-next " aria-label="Next page: URI处理">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"集合","level":"1.4.2","depth":2,"next":{"title":"URI处理","level":"1.4.3","depth":2,"path":"URI处理.md","ref":"URI处理.md","articles":[]},"previous":{"title":"字符串","level":"1.4.1","depth":2,"path":"字符串.md","ref":"字符串.md","articles":[]},"dir":"ltr"},"config":{"plugins":["anchor-navigation-ex","expandable-chapters-small","-lunr","-search","search-pro","-sharing","github","expandable-chapters","pageview-count","theme-comscore","tbfed-pagefooter","popup","custom-favicon"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy Arcticfox 2020","modify_label":"文档修订于：","modify_format":"YYYY-MM-DD HH:mm:ss"},"github":{"url":"https://github.com/arcticfox1919"},"search-pro":{},"popup":{},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"anchor-navigation-ex":{"associatedWithSummary":true,"float":{"floatIcon":"fa fa-navicon","level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"mode":"float","multipleH1":true,"pageTop":{"level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"printLog":false,"showGoTop":true,"showLevel":true},"favicon":"logo.ico","theme-comscore":{},"pageview-count":{},"custom-favicon":{},"expandable-chapters-small":{},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"expandable-chapters":{}},"theme":"default","author":"arcticfox","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"Dart编程指南","language":"zh-hans","links":{"sidebar":{"主页":"http://www.bczl.xyz/"}},"gitbook":"*"},"file":{"path":"集合.md","mtime":"2021-10-18T12:50:20.554Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2021-10-18T14:58:03.414Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-github/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-pageview-count/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-theme-comscore/test.js"></script>
        
    

    </body>
</html>

